//
//  CPSCVerticalAndTransverseMarkView.m
//  CollTest
//
//  Created by codew on 11/22/21.
//

#import "CPSCVerticalAndTransverseMarkView.h"
#import "CPSCUIFloatLayoutView.h"
#import "CPSCGradualChangeBtn.h"

typedef NS_ENUM(NSInteger, CPSCMarkViewType) {
    /** 横向 */
    CPSCMarkViewType_Transverse,
    /** 竖向 */
    CPSCMarkViewType_Vertical
};


@interface CPSCVerticalAndTransverseMarkView ()

@property (nonatomic, strong) CPSCUIFloatLayoutView * viewConten;

@property (nonatomic, assign) BOOL isUpdateView;
@property (nonatomic, strong) NSArray <NSString *>* titleArray;

///** 横向/竖向 */
@property (nonatomic, assign) CPSCMarkViewType type;

@property (nonatomic, strong) NSMutableArray <CPSCGradualChangeBtn *> * btnUIArrayTransverse;
@property (nonatomic, strong) NSMutableArray <CPSCGradualChangeBtn *> * btnUIArrayVertical;


@end

@implementation CPSCVerticalAndTransverseMarkView


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        
        [self initSubviews];
        [self makeContraints];
    }
    return self;
}


- (void)initSubviews
{
    
    [self addSubview:self.viewConten];
    [self deBugUI];
}

- (NSMutableArray<CPSCGradualChangeBtn *> *)btnUIArrayTransverse
{
    if (!_btnUIArrayTransverse) {
        
        _btnUIArrayTransverse = [NSMutableArray array];
    }
    
    return _btnUIArrayTransverse;
}

- (NSMutableArray<CPSCGradualChangeBtn *> *)btnUIArrayVertical
{
    if (!_btnUIArrayVertical) {
        
        _btnUIArrayVertical = [NSMutableArray array];
    }
    
    return _btnUIArrayVertical;
}

- (void)makeContraints {
    
    [self.viewConten mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];

}

- (void)deBugUI
{
    //    self.viewHtmlContentSuper.backgroundColor = kCPSCRandomColor;
    //    self.lblContent.backgroundColor = kCPSCRandomColor;
}

- (void)updateVerticalViewWithTitles:(NSArray <NSString *>*)titleArray;
{
    self.isUpdateView = YES;
    self.type = CPSCMarkViewType_Vertical;
    self.titleArray = titleArray;

    [self updateContent];

}

/** 横向 */
- (void)updateTransverseViewWithTitles:(NSArray <NSString *>*)titleArray
{   self.isUpdateView = YES;
    self.type = CPSCMarkViewType_Transverse;
    self.titleArray = titleArray;

    [self updateContent];
}


- (void)updateContentVertical
{
    self.viewConten.itemMargins = UIEdgeInsetsMake(4, 10, 0, 0);
    self.viewConten.minimumItemSize = CGSizeMake(32, 14);
    self.viewConten.maximumItemSize = CGSizeMake(58, 14);
    self.viewConten.isOnlyOneInRow = YES;
    self.viewConten.isItemsCenter = YES;
    
    if (self.btnUIArrayVertical.count != self.titleArray.count) {
        
        [self.btnUIArrayVertical removeAllObjects];
        [self updateContentVerticalArr];
    }else{
        
        [self.viewConten.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for (int i = 0; i < self.titleArray.count; i++) {
            
            NSString *titile = self.titleArray[i];
            CPSCGradualChangeBtn * btn = self.btnUIArrayVertical[i];
            [btn setTitle:titile forState:UIControlStateNormal];
            [self.viewConten addSubview:btn];
            [btn sizeToFit];
        }
    }
}

- (void)updateContentVerticalArr
{
    [self.viewConten.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (NSString *titile in self.titleArray) {
        
        
        CPSCGradualChangeBtn * btn = [[CPSCGradualChangeBtn alloc] init];
        
        [btn setTitleColor:[UIColor colorWithHexString:@"#947E4C"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:10];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
//        [btn addTarget:self action:@selector(btnAskClick) forControlEvents:UIControlEventTouchUpInside];
        btn.userInteractionEnabled = NO;
        
//        [btn setBackgroundColor:UIColor.yellowColor];
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 4);
        
        btn.layer.cornerRadius = 2;
        btn.layer.borderColor = [UIColor colorWithHexString:@"#DAC79A"].CGColor;
        btn.layer.borderWidth = 0.5;
        btn.layer.masksToBounds = YES;
        
        [btn setBackgroundImage:[UIImage imageNamed:@"category_jb"] forState:UIControlStateNormal];
        
        

        
        [btn setTitle:titile forState:UIControlStateNormal];
        btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
        [self.viewConten addSubview:btn];
        [self.btnUIArrayVertical addObject:btn];
        [btn sizeToFit];

    }
}

- (void)updateContentTransverse
{
//    self.viewConten.itemMargins = UIEdgeInsetsMake(4, 10, 0, 0);
    self.viewConten.minimumItemSize = CGSizeMake(10, 14);
    self.viewConten.maximumItemSize = CGSizeMake(100, 14);
    self.viewConten.itemMargins = UIEdgeInsetsMake(10, 4, 0, 0);
    self.viewConten.clipsToBounds = YES;
    [self.viewConten.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (NSString *titile in self.titleArray) {
        
        
        CPSCGradualChangeBtn * btn = [[CPSCGradualChangeBtn alloc] init];
        
        [btn setTitleColor:[UIColor colorWithHexString:@"#5C7499"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:10];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.userInteractionEnabled = NO;
        
        [btn setBackgroundColor:[UIColor colorWithHexString:@"#F0F2F5"]];
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 3);
        
        btn.layer.cornerRadius = 1;
        btn.layer.masksToBounds = YES;
                
        
        [btn setTitle:titile forState:UIControlStateNormal];
        btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
        [self.viewConten addSubview:btn];
        [btn sizeToFit];

    }
}
- (void)updateContent
{
    
    if (self.type == CPSCMarkViewType_Vertical) {
        
        [self updateContentVertical];
    }else{
        
        [self updateContentTransverse];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.isUpdateView && self.titleArray.count > 0) {

        self.isUpdateView = NO;
        [self updateContent];
    }
    
}


#pragma mark - Getter Setter

- (CPSCUIFloatLayoutView *)viewConten
{

    if (!_viewConten) {
        
        CPSCUIFloatLayoutView * view = [[CPSCUIFloatLayoutView alloc] init];
        // UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
        //        CGSizeMake(<#CGFloat width#>, <#CGFloat height#>)
//        view.itemMargins = UIEdgeInsetsMake(10, 10, 0, 0);
//        view.minimumItemSize = CGSizeMake(0, 14);
//        view.maximumItemSize = CGSizeMake(80, 14);
//        view.backgroundColor = [UIColor redColor];
        
        view.clipsToBounds = YES;
        _viewConten = view;
    }
    return _viewConten;
}



@end
